package com.atguigu.controller;


import com.atguigu.client.SearchFeignClient;
import com.atguigu.constant.MqConst;
import com.atguigu.entity.ProductImage;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuInfo;
import com.atguigu.result.RetVal;
import com.atguigu.service.ProductImageService;
import com.atguigu.service.ProductSalePropertyKeyService;
import com.atguigu.service.SkuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author zhangqiang
 * @since 2021-08-24
 */
@RestController
@RequestMapping("/product")
public class SkuController {
    @Autowired
    private ProductSalePropertyKeyService salePropertyKeyService;
    @Autowired
    private ProductImageService productImageService;
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private SearchFeignClient searchFeignClient;
    //1.根据商品spu的id查询销售属性 http://10.211.55.98/product/querySalePropertyByProductId/16
    @GetMapping("querySalePropertyByProductId/{productId}")
    public RetVal querySalePropertyByProductId(@PathVariable Long productId){
        List<ProductSalePropertyKey> salePropertyKeyList=salePropertyKeyService.querySalePropertyByProductId(productId);
        return RetVal.ok(salePropertyKeyList);
    }

    //2.根据商品spu的id查询productImage里面的图片信息 http://10.211.55.98/product/queryProductImageByProductId/16
    @GetMapping("queryProductImageByProductId/{productId}")
    public RetVal queryProductImageByProductId(@PathVariable Long productId){
        QueryWrapper<ProductImage> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",productId);
        List<ProductImage> productImageList = productImageService.list(wrapper);
        return RetVal.ok(productImageList);
    }
    //3.保存商品sku信息 http://127.0.0.1/product/saveSkuInfo
    @PostMapping("saveSkuInfo")
    public RetVal saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuInfoService.saveSkuInfo(skuInfo);
        return RetVal.ok();
    }

    //4.sku列表的查询 http://127.0.0.1/product/querySkuInfoByPage/1/10
    @GetMapping("querySkuInfoByPage/{currentPageNum}/{pageSize}")
    public RetVal querySkuInfoByPage(@PathVariable Long currentPageNum,@PathVariable Long pageSize){
        Page<SkuInfo> skuInfoPage = new Page<>(currentPageNum, pageSize);
        //查询条件
        QueryWrapper<SkuInfo> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("id");
        skuInfoService.page(skuInfoPage,wrapper);
        return RetVal.ok(skuInfoPage);
    }
    //5.sku的上架 http://127.0.0.1/product/onSale/38
    @GetMapping("onSale/{skuId}")
    public RetVal onSale(@PathVariable Long skuId){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        //是否销售（1：是 0：否）
        skuInfo.setIsSale(1);
        skuInfoService.updateById(skuInfo);
        //远程调用shop-search
        //searchFeignClient.onSale(skuId);
        rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE,MqConst.ON_SALE_ROUTING_KEY,skuId);
        return RetVal.ok();
    }
    //6.sku的下架 http://127.0.0.1/product/offSale/38
    @GetMapping("offSale/{skuId}")
    public RetVal offSale(@PathVariable Long skuId){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        //是否销售（1：是 0：否）
        skuInfo.setIsSale(0);
        skuInfoService.updateById(skuInfo);
        //远程调用shop-search
        //searchFeignClient.offSale(skuId);
        rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE,MqConst.OFF_SALE_ROUTING_KEY,skuId);
        return RetVal.ok();
    }





}

